
public class CategoryAnalyst 
{
	/*This class performs a number of helpful functions to help you
	 * interface nicely with the string representation of the supertag.
	 */
	
	public int calcNumOfArgs(String input)
	{
		return calcNumOfArgsHelper(input, 0);
	}
	
	/*public String filterTildes(String input)
	{
		for(int a = 0; a < input.length(); a++)
		{
			
		}
	}
	*/
	public String addSemanticInfo(String input)
	{
		int z = 1;
		String result = "";
		for(int a = 0; a < input.length(); a++)
		{
			if(input.charAt(a) == '/' || input.charAt(a) == '\\' || input.charAt(a) == '(' || input.charAt(a) == ')')
			{
				char prev = result.charAt(result.length()-1);
				if(prev != ')' && prev != '/' && prev != '\\')
				{
					result += "_" + z;
					z++;
				}
			}
			result+= input.charAt(a);
		}
		if(result.charAt(result.length()-1) != ')')
			result += "_" + z;
		return result;
	}
	
	public String cleanSupertag(String input)
	{
		String result = "";
		for(int a = 0; a < input.length(); a++)
		{
			char spotlight = input.charAt(a);
			if(spotlight != '_' &&
					spotlight != '~' &&
					spotlight != '$' &&
					!Character.isDigit(spotlight))
				result += spotlight;
		}
		return result;
	}
	
	public String eliminateSemanticInfo(String input)
	{
		String result = "";
		for(int a = 0; a < input.length(); a++)
		{
			//System.out.println(a);
			//System.out.println(input.charAt(a));
			if(input.charAt(a) == '_')
			{
				a++;
				//System.out.println(input.charAt(a));
				while(a < input.length() && isNumber(input.charAt(a)))
				{
				//	System.out.println("click");
					a++;
				}
				a--;
			}
			else
			{
				result += input.charAt(a);
			}
		}
		return result;
	}
	
	public boolean isNumber(char c)
	{
		return(c == '1' || c == '2' || c == '3' || c == '4' || c == '5' || c == '6' || c == '7' || c == '8' || c == '9' || c == '0');
	}
	
	
	private int calcNumOfArgsHelper(String input, int num)
	{
		String garbage = removeOutsideParens(getConstituent(input, 0));
		
		if(garbage.equals(input))
		{
			return num;
		}
		else
		{
			return calcNumOfArgsHelper(garbage, num+1);
		}
	}
	
	public boolean isArgument(String functor, String functee) throws Exception
	{
		functor = removeOutsideParens(functor);
		String base = getConstituent(functor,0);
		if(base == null)
			throw new Exception("Bad supertag format (1).");
		
		int matrixSlashIndex = base.length();
		String arg = getConstituent(functor,matrixSlashIndex+1);
		if(arg == null)
			throw new Exception("Bad supertag format (2).");
		
		arg = removeOutsideParens(arg);
		String functee2 = removeOutsideParens(functee);
		if(arg.equals(functee2))
			return true;
		else return false;
		
	}
	
	
	public boolean isAdjunct(String input) throws Exception
	{
		input = removeOutsideParens(input);
		String temp1 = getConstituent(input,0);
		if(temp1 == null)
			throw new Exception("Bad supertag format (1).");
		
		int matrixSlashIndex = temp1.length();
		String temp2 = getConstituent(input,matrixSlashIndex+1);
		if(temp2 == null)
			throw new Exception("Bad supertag format (2).");
		
		temp1= removeOutsideParens(temp1);
		temp2 = removeOutsideParens(temp2);
		
		System.out.println("temp1: " + temp1);
		System.out.println("temp2: " + temp2);
		
		if(temp1.equals(temp2))
			return true;
		else return false;
	}
	
	public String removeOutsideParens(String input)
	{
		if(input.length() <= 2)
			return input;
		
		int numLP = 0;
		int numRP = 0;
		boolean abort = false;
		
		int a =0;
		if(input.charAt(0) == '(')
		{
			numLP++;
			for(a= 1; a < input.length() && !abort; a++)
			{
				if(input.charAt(a) == '(')
					numLP++;
				else if(input.charAt(a) == ')')
					numRP++;
				if(numLP == numRP)
					abort = true;
			}
		}
		if(a == input.length())
			return input.substring(1,input.length()-1);
		else return input;
	}
	
	
	public String getConstituent(String input, int startIndex)
	{
		int numLP = 0;
		int numRP = 0;
		boolean abort = false;
		
		if(input.charAt(startIndex) == '(')
		{
			numLP++;
			int a;
			for(a = startIndex + 1; a< input.length() && !abort; a++)
			{
				if(input.charAt(a) == '(')
					numLP++;
				else if(input.charAt(a) == ')')
					numRP++;
				
				if(numLP == numRP)
					abort = true;
			}
			return input.substring(startIndex, a);
		}
		else
		{
			int a;
			for(a = startIndex+1; a< input.length() && !abort; a++)
			{
				if(input.charAt(a) == '/' || input.charAt(a) == '\\')
					abort = true;
			}
			if(abort)
				return input.substring(startIndex,a-1);
			else return input.substring(startIndex,a);
		}
	}
	
	public int getIndexOfMatrixSlash(String input)
	{
		String temp = getConstituent(input,0);
		if(temp.length() < input.length())
			return temp.length();
		else return -1;
		
	}
	
	public String getMatrixSlash(String input)
	{
		int t = getIndexOfMatrixSlash(input);
		if(t < 0)
			return "NOSLASH"; //don't return null to avoid ugly null-pointer issues
		return input.substring(t,t+1);
	}
}
